﻿using System;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using SedionFormDesigner.Designer;

namespace SedionFormDesigner.SedienFormDesigner.Infrastructure
{
    /// <summary>
    ///  接收键盘按钮事件
    /// </summary>
    public class KeystrokeMessageFilter : System.Windows.Forms.IMessageFilter
    {
        private IDesignerHost host;
        public IDesignerHost Host
        {
            get
            {
                return host;
            }
            set
            {
                this.host = value;
            }
        }

        #region IMessageFilter 成员

        public bool PreFilterMessage(ref System.Windows.Forms.Message m)
        {
            // 捕捉WM_KEYCHAR属性,如果designerView已经聚焦了的话
            if ((m.Msg == 0x0100) && (((SedionDesignerHost)host).View.Focused))
            {
                IMenuCommandService mcs = host.GetService(typeof(IMenuCommandService)) as IMenuCommandService;

                // WM_KEYCHAR 只能获得最后按下的键值
                // 所以还要检测Control和Shift
                switch (((Int32)m.WParam) | ((Int32)Control.ModifierKeys))
                {
                    case (Int32)Keys.Delete:
                        mcs.GlobalInvoke(MenuCommands.Delete);
                        break;
                    case (Int32)Keys.Up:
                        mcs.GlobalInvoke(MenuCommands.KeyMoveUp);
                        break;
                    case (Int32)Keys.Down:
                        mcs.GlobalInvoke(MenuCommands.KeyMoveDown);
                        break;
                    case (Int32)Keys.Right:
                        mcs.GlobalInvoke(MenuCommands.KeyMoveRight);
                        break;
                    case (Int32)Keys.Left:
                        mcs.GlobalInvoke(MenuCommands.KeyMoveLeft);
                        break;
                    case (Int32)(Keys.Control | Keys.Up):
                        mcs.GlobalInvoke(MenuCommands.KeyNudgeUp);
                        break;
                    case (Int32)(Keys.Control | Keys.Down):
                        mcs.GlobalInvoke(MenuCommands.KeyNudgeDown);
                        break;
                    case (Int32)(Keys.Control | Keys.Right):
                        mcs.GlobalInvoke(MenuCommands.KeyNudgeRight);
                        break;
                    case (Int32)(Keys.Control | Keys.Left):
                        mcs.GlobalInvoke(MenuCommands.KeyNudgeLeft);
                        break;
                    case (Int32)(Keys.Shift | Keys.Up):
                        mcs.GlobalInvoke(MenuCommands.KeySizeHeightIncrease);
                        break;
                    case (Int32)(Keys.Shift | Keys.Down):
                        mcs.GlobalInvoke(MenuCommands.KeySizeHeightDecrease);
                        break;
                    case (Int32)(Keys.Shift | Keys.Right):
                        mcs.GlobalInvoke(MenuCommands.KeySizeWidthIncrease);
                        break;
                    case (Int32)(Keys.Shift | Keys.Left):
                        mcs.GlobalInvoke(MenuCommands.KeySizeWidthDecrease);
                        break;
                    case (Int32)(Keys.Control | Keys.Shift | Keys.Up):
                        mcs.GlobalInvoke(MenuCommands.KeyNudgeHeightIncrease);
                        break;
                    case (Int32)(Keys.Control | Keys.Shift | Keys.Down):
                        mcs.GlobalInvoke(MenuCommands.KeyNudgeHeightDecrease);
                        break;
                    case (Int32)(Keys.Control | Keys.Shift | Keys.Right):
                        mcs.GlobalInvoke(MenuCommands.KeyNudgeWidthIncrease);
                        break;
                    case (Int32)(Keys.ControlKey | Keys.Shift | Keys.Left):
                        mcs.GlobalInvoke(MenuCommands.KeyNudgeWidthDecrease);
                        break;
                    case (Int32)(Keys.Escape):
                        mcs.GlobalInvoke(MenuCommands.KeyCancel);
                        break;
                    case (Int32)(Keys.Shift | Keys.Escape):
                        mcs.GlobalInvoke(MenuCommands.KeyReverseCancel);
                        break;
                    case (Int32)(Keys.Enter):
                        mcs.GlobalInvoke(MenuCommands.KeyDefaultAction);
                        break;
                }
            }
            // 永远不匹配消息
            return false;
        }

        #endregion
    }
}
